package com.yksj.healthtalk.ui.buyandsell;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.recharge.ChoiseDateActivity;
import com.yksj.healthtalk.utils.TimeUtil;

/**
 * 药品详细推荐次数页面
 * 
 * @author root
 * 
 */
public class RecommentOnlickCount extends BaseFragmentActivity implements
		OnClickListener {
	private String date="";
	private TextView onclick_count;
	private TextView tuijian_count;
	private TextView buy_count;
	private TextView nowTime;//日历当前显示时间

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.recommendation_onlickcount);
		initTitle();
		initView();
		initData();
	}

	private void initView() {
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("统计");
		onclick_count=(TextView)findViewById(R.id.onclick_count);
		tuijian_count=(TextView)findViewById(R.id.tuijian_count);
		buy_count=(TextView)findViewById(R.id.buy_count);
		
		findViewById(R.id.date_icon).setOnClickListener(this);
		
		nowTime = (TextView)findViewById(R.id.date_text);
		nowTime.setOnClickListener(this);
		nowTime.setText(TimeUtil.getMoneyTime().substring(0,4)+"");

	}

	private void initData() {
		HttpRestClient.doHttpRecommentOnlickCount(date,SmartFoxClient.getLoginUserId(),"","" ,new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, String content) {
				super.onSuccess(statusCode, content);
				String[] queryCount = content.split(",");
				tuijian_count.setText(queryCount[0]);
				onclick_count.setText(queryCount[1]);
				buy_count.setText(queryCount[2]);
				
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode!=RESULT_OK)return;
		switch (requestCode) {
		case 1000:
			if(data!=null){
				Bundle extras = data.getExtras();
				if(extras.containsKey("nothing"))return;
				String string = extras.getString("date");
				String string1 = extras.getString("year");
				nowTime.setText(string1+"-"+string);
				date=string1+string;
				}else{
					date=null;
					nowTime.setText(TimeUtil.getMoneyTime().substring(0,4)+"");
				}
				initData();
			break;
		}
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			overridePendingTransition(R.anim.back_enter,R.anim.back_exit);
			break;
		case R.id.date_icon:
		case R.id.date_text:
			Intent intent=new Intent(this,ChoiseDateActivity.class);
			if(nowTime.getText().toString().length()<5){
				intent.putExtra("date", TimeUtil.getMoneyTime());
			}else{
				intent.putExtra("date", nowTime.getText().toString());
			}
			startActivityForResult(intent,1000);
			break;
		}
	}
}
